#include <bits/stdc++.h>
// 2025/01/19
// tag:
// Author: Zhang Muen
using namespace std;

int64_t n, m, dist[1501];
unordered_map<int64_t, int64_t> graph[1501];
bool vis[1501];

void SPFA(int64_t start)
{
    for (int i = 1; i <= n; i++)
        dist[i] = INT_MAX;
    queue<int> q;
    dist[start] = 0, vis[start] = true;
    q.push(start);
    while (!q.empty())
    {
        int u = q.front();
        q.pop(), vis[u] = false;
        for (auto neighbor : graph[u])
        {
            int v = neighbor.first, w = neighbor.second;
            if (dist[v] > dist[u] + w){
                dist[v] = dist[u] + w;
                if (!vis[v])
                    q.push(v), vis[v] = true;
            }
        }
    }
}

signed main()
{
    cin >> n >> m;
    int64_t u, v, w;
    for (int i = 0; i < m; i++)
    {
        cin >> u >> v >> w;
        if (graph[u].find(v) != graph[u].end())
            graph[u][v] = min(graph[u][v], -w);
        else
            graph[u][v] = -w;
    }
    SPFA(1);
    cout << (dist[n] == INT_MAX ? -1 : -dist[n]);
    return 0;
}